﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ProgNetDxComponents.Controls.Forms
{
    public partial class InputFormCombo : InputFormBase
    {
        private InputFormCombo()
        {
            InitializeComponent();
        }
        private Dictionary<string, object> _source = new Dictionary<string, object>();

        public InputFormCombo(DataTable source, string valueColumn, string displayColumn, string filter = null) :this()
        {
            DataRow[] rows = source.Select(filter, null, DataViewRowState.CurrentRows);
            string s;
            foreach (DataRow r in rows)
            {
                s = r[displayColumn].AsString();
                if (!_source.ContainsKey(s))
                    _source.Add(s, r[valueColumn]);
            }
        }
        public InputFormCombo(ICollection collection) : this()
        {
            string s;
            foreach (object o in collection)
            {
                s = o.AsString();
                if (!_source.ContainsKey(s))
                    _source.Add(s, o);
            }
        }

        public override object GetEditValue()
        {
            string s = comboBoxEdit1.EditValue.AsString();
            if (_source.ContainsKey(s))
                return _source[s];
            return null;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            comboBoxEdit1.Properties.Items.AddRange(_source.Keys);
        }

        public static ObjectDialogResult ShowInputDialog(string caption, string text, DataTable source, string valueColumn, string displayColumn, string filter = null)
        {
            InputFormCombo f = new InputFormCombo(source, valueColumn, displayColumn, filter);
            ObjectDialogResult res = f.ShowInputDialog(caption, text);
            f.Dispose();
            return res;
        }
        public static ObjectDialogResult ShowInputDialog(string text, DataTable source, string valueColumn, string displayColumn, string filter = null)
        {
            return ShowInputDialog(Application.ProductName, text, source, valueColumn, displayColumn, filter);
        }
        public static ObjectDialogResult ShowInputDialog(string caption, string text, ICollection collection)
        {
            InputFormCombo f = new InputFormCombo(collection);
            ObjectDialogResult res = f.ShowInputDialog(caption, text);
            f.Dispose();
            return res;
        }
        public static ObjectDialogResult ShowInputDialog(string text, ICollection collection)
        {
            return ShowInputDialog(Application.ProductName, text, collection);
        }
    }
}
